import yaml
from appium import webdriver

with open(r"D:\app_auto_test\Base\driver_info.yaml", "r", encoding="utf-8") as f:
    data = yaml.load(f)


# 生成driver的函数，在Scripts脚本中实例化测试的时候传入
def base_driver(device: str, no_reset: bool):
    desired_caps = {}
    desired_caps["platformName"] = data[device]["platformName"]
    desired_caps["platformVersion"] = data[device]["platformVersion"]
    desired_caps["udid"] = data[device]["udid"]
    desired_caps["deviceName"] = data[device]["deviceName"]
    desired_caps["appPackage"] = data[device]["appPackage"]
    desired_caps["appActivity"] = data[device]["appActivity"]
    desired_caps["newCommandTimeout"] = data[device]["newCommandTimeout"]
    desired_caps["noReset"] = no_reset
    driver = webdriver.Remote("http://127.0.0.1:" + data[device]["port"] + "/wd/hub", desired_caps)
    driver.implicitly_wait(15)
    return driver


def launch_driver(device: str, no_reset: bool):
    desired_caps = {}
    desired_caps["platformName"] = data[device]["platformName"]
    desired_caps["platformVersion"] = data[device]["platformVersion"]
    desired_caps["udid"] = data[device]["udid"]
    desired_caps["deviceName"] = data[device]["deviceName"]
    desired_caps["appPackage"] = data[device]["launch_appPackage"]
    desired_caps["appActivity"] = data[device]["launch_appActivity"]
    desired_caps["newCommandTimeout"] = data[device]["newCommandTimeout"]
    desired_caps["noReset"] = no_reset
    driver = webdriver.Remote("http://127.0.0.1:" + data[device]["port"] + "/wd/hub", desired_caps)
    driver.implicitly_wait(15)
    return driver
